<template>
    <div class="container">
        <div class="row">
            <div class="col">
                <h3>{{topic.title}}</h3>
                <p>{{topic.body}}</p>
                <Form>
                    <FormItem label="评论">
                        <Input type="textarea" v-model="reply"/>
                    </FormItem>
                </Form>
                <Button type="success" @click="comment">评论</Button>
                <ul class="list-unstyled">
                    <li class="media mt-4 mb-2" v-for="reply in replies">
                        <Avatar  class="mr-3" :src="avatarUrl(reply.user.avatar)"/>
                        <div class="media-body">
                            <p>{{reply.content}}</p>
                            <span>{{reply.updated_time}}</span>

                            <Divider />
                        </div>
                    </li>
                </ul>

                <Page :total="total" show-sizer v-if="replies.length > 0" :current="current_page"/>
            </div>
        </div>
    </div>
</template>

<script>
    import  avatar from '../../utils/urlutils';
    import {mapState} from 'vuex';
    export default {
        name: "PostDetail",
        mounted() {
            this.id = this.$route.params.id;
            this.getPostDetail();
            this.getReplies();
        },
        computed: {
            ...mapState('user',['user'])
        },
        methods: {
            comment() {
                let params = new URLSearchParams();
                params.append('content',this.reply);
                params.append('user_id',1);
                params.append('post_id',this.id);
                params.append('parent_id',0);
                http.post('/api/reply',params).then((data)=>{
                    console.log(data);
                    this.reply = ''
                    if(data.code === 200) {
                        this.$Message.info('评论成功!');
                        this.replies.unshift(data.data)
                    }

                });
            },
            avatarUrl(url) {
                return avatar(url)
            },
            getPostDetail() {
                http.get('/api/post/'+this.id).then((data)=>{
                    console.log(data)
                    this.topic = data.data;
                });
            },
            getReplies() {
                http.get('/api/reply',{
                    params: {
                        post_id: this.id,
                        with_page:true,
                        current_page:this.current_page
                    }
                }).then((data)=>{
                    this.replies = data.data.data;
                    this.total = data.data.total;
                    this.current_page = data.data.current_page;
                });
            }
        },
        data() {
            return {
                id: '',
                topic: '',
                reply: '',
                replies: [],
                current_page:1,
                total:0
            }
        }
    }
</script>

<style scoped>

</style>
